package com.wskj.shiro.chapter2.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;

public class MyRealm1 implements Realm {

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		// 返回配置的名称
		return "myRealm1";
	}

	
	@Override
	public boolean supports(AuthenticationToken token) {
		// TODO Auto-generated method stub
		// 仅仅支持  用户名和密码的类型的验证
		return token instanceof UsernamePasswordToken;
	}

	
	// 进制验证 
	@Override
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		// TODO Auto-generated method stub
		
		String username =(String) token.getPrincipal();//用户名称
		String password=new String((char[]) token.getCredentials());//用户的密码
		
		if(!"zhang".equals(username)) {  
            throw new UnknownAccountException(); //如果用户名错误  
        }  
        if(!"123".equals(password)) {  
            throw new IncorrectCredentialsException(); //如果密码错误  
        }  
        
        //如果身份认证验证成功，返回一个AuthenticationInfo实现；		
		return new SimpleAuthenticationInfo(username, password, getName());
	}

}
